package com.fth.learn.chapter07;

import java.lang.reflect.Array;
import java.util.Arrays;

import static jdk.nashorn.internal.objects.NativeArray.reverse;

/**
 * 字符串练习题
 * YiMi
 *
 * @author YiMi
 * @version 1.0
 * @date 2023/7/18 8:02
 */


public class homework01 {
    //统计字符串“abc21de416f”中数字个数
    public static void question01(){
        String s="abc21de416f";
        char[] s1 = s.toCharArray();
        System.out.println(Arrays.toString(s1));
        int num=0;
        for (int i = 0; i < s1.length; i++) {
            if(s1[i]>='0'&&s1[i]<='9'){
                num++;
            }
        }
        System.out.println("数字的个数是："+num);

    }

    //设计一个方法，将给定字符串的正序和反序进行连接 ok—>okko
    public static void question02(){
//        String s2="ok";
//        String s2= new String("ok");
//        System.out.println(s2.reverse());
//        System.out.println(reverse(s2));
//            s2.reverse();
        StringBuffer s2=new StringBuffer("ok");
        String s5 = String.valueOf(s2);
        String s3= String.valueOf(s2.reverse());
        String s4 = s5+s3;
        System.out.println(s4);
    }

    //设计一个方法，统计“a”在字符串“this is a test of java”中出现的次数
    public static void question03(){
        String s6 = "this is a test of java";
        char[] arr = s6.toCharArray();
        int num=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]=='a'){
                num++;
            }
        }
        System.out.println("a有:"+num+"个");
    }

    //设计一个方法，将给定的字符串每一个首字母大写 “this is a test of java”---->“This Is A Test Of Java”
    public static void question04(){
        String s7 =  "this is a test of java";
        char[] arr = s7.toCharArray();
        int num=0;
        for (int i = 0; i < arr.length; i++) {
            arr[0] = Character.toUpperCase(arr[0]);
            if(arr[i] == ' '){
                arr[i + 1] = Character.toUpperCase(arr[i + 1]);
            }
        }
//        String string = String.copyValueOf(arr);
//        System.out.println(string);
        System.out.println(arr);

    }

    //设计一个方法，获取给定字符串中的全部数字 “za1o1zbp24tcq”
    public static void question05(){
        String s8 = "za1o1zbp24tcq";
        int num=0;
        char s= '0';
        for (int i = 0; i < s8.length(); i++) {
            if(s8.charAt(i) >='0'&&s8.charAt(i)<='9'){
                num++;
                s=s8.charAt(i);
                System.out.printf("数字是：");
                System.out.printf(String.valueOf(s));
            }
        }
        System.out.println();
        System.out.println("数字有："+num+"个");
    }

    //编写程序在将字符串 “want you to know one thing”, 统计出字母 “n”和 字母 “o”的出现次数
    public static void question06(){
        String s9 = "want you to know one thing";
        int num=0;
        char s= '0';
        for (int i = 0; i < s9.length(); i++) {
            if(s9.charAt(i) =='n'){
                num++;
            }
        }
        System.out.println("n出现了"+num+"次");
        for (int i = 0; i < s9.length(); i++) {
            if(s9.charAt(i) =='o'){
                num++;
            }
        }
        System.out.println("o出现了"+num+"次");


    }



//    "轻轻的我走了， 正如我轻轻的来；我轻轻的招手，作别西天的云彩。"最后一次出现"轻"的位置
    public static void question07(){
        String s10 = "轻轻的我走了，正如我轻轻的来；我轻轻的招手，作别西天的云彩。";
        int num = 0;
        for (int i = 0; i < s10.length(); i++) {
            if(s10.indexOf("轻",num) !=-1){
                num=i;
            }
        }
        System.out.println(num);
    }




//    求字符串“powerful heaven dragon”中有多少种字符





//    字符串“peter piper picked a peck of pickled peppers”中有多少以p开头的单词
        public static void question09() {
            String s12 = "peter piper picked a peck of pickled peppers";

            char[] arr = s12.toCharArray();
            int num = 0;
            if (arr[0] == 'p') {
                num = num+1;
            }
            for (int i = 0; i < arr.length-1; i++) {
                if (arr[i] ==' '&& arr[i + 1] == 'p') {
                    num = num+1;
                }
            }
            System.out.println(num);

        }

//编写程序将“jdk”全部变为大写，并输出到屏幕，截取子串“DK”，并输出到屏幕
        public static void question10() {
            String s13 = "jdk";
            String s14 = s13.toUpperCase();
            System.out.println(s14);
            String s15 =  s14.substring(1);
            System.out.println(s15);
        }

        //编写程序将String类型字符串“test”变为“tset”
    public static void question11() {
        StringBuffer s16=new StringBuffer("test");
        String.valueOf(s16.reverse());
        System.out.println(s16);
    }







    public static void main(String[] args) {
//        question01();
//        question02();
//        question03();
//        question04();
//        question05();
//        question06();
//        question07();
//        question09();
        question10();
        question11();
    }
}
